<template>
    <div class="p-detail-box" v-if="form">
        <van-nav-bar title="资产盘点" left-arrow @click-left="$router.back()" />
        <van-tabs class="new-tabs-box" scrollable swipe-threshold="4">
            <van-tab v-for="(item, index) in tabTit" :title="item.title" :key="index">
                <van-form>
                    <div v-for="(child, j) in getFieldListByTitle(item.title)" class="item-box item-box1" :key="j">
                        <template v-if="item.title === '基本信息'">
                            <template v-if="child.code === 'jcxx' && child.isShow && !!child.display">
                                <div class="s-name">{{ child.name }}</div>
                                <div v-for="(c, g) in child.childList" :key="g" class="bb1">
                                    <van-field v-model="form.code" readonly :label="c.name" v-if="c.code === 'zcbm' && c.isShow && !!c.display" />
                                    <van-field v-model="form.orgName" readonly :label="c.name" v-if="c.code === 'gldw' && c.isShow && !!c.display" />
                                    <van-field v-model="form.projectName" readonly :label="c.name" right-icon="arrow" v-if="c.code === 'ssxm' && c.isShow && !!c.display" />
                                    <van-field v-model="form.typeName" readonly @click-right-icon="goZclx" right-icon="arrow" :label="c.name" v-if="c.code === 'zclx' && c.isShow && !!c.display" />
                                    <van-field v-model="form.nodeName" readonly :label="c.name" v-if="c.code === 'zcwz' && c.isShow && !!c.display" />
                                    <van-field v-model="form.purposeNames" readonly :label="c.name" v-if="c.code === 'zcyt' && c.isShow && !!c.display" />
                                    <van-field v-model="form.relatedNames" readonly  @click-right-icon="goGlzc" right-icon="arrow" :label="c.name" v-if="c.code === 'glzc' && c.isShow && !!c.display" />
                                    <van-field v-model="form.remark" readonly :label="c.name" v-if="c.code === 'bz' && c.isShow && !!c.display" />
                                    <van-field v-model="form.userName" readonly :label="c.name" v-if="c.code === 'zcsyr' && c.isShow && !!c.display" />
                                    <van-field v-model="form.countAmount" readonly :label="c.name" v-if="c.code === 'tjsl' && c.isShow && !!c.display" />
                                    <van-field v-if="c.code === 'zctp' && shouldShowField(c)" class="disn" />
                                    <template v-if="+form.category === 0">
                                        <van-field v-model="form.houseOrlandDetailWebObj.houseNumber" readonly :label="c.name" v-if="c.code === 'fh' && shouldShowField(c)" />
                                    </template>
                                </div>
                                <ZdyView :zdyArryList="child.childList" :title="item.title" v-if="!!child.display" />
                            </template>
                            <template v-else-if="child.code === 'wzxx' && child.isShow && !!child.display">
                                <div class="s-name">{{ child.name }}</div>
                                <div v-for="(c, g) in child.childList" :key="g" class="bb1">
                                    <van-field v-model="form.coordinateAddress" @click-right-icon="goZcwz" right-icon="location-o" readonly :label="c.name" v-if="c.code === 'xmdz' && c.isShow && !!c.display" />
                                    <van-field v-model="form.streetName" readonly :label="c.name" v-if="c.code === 'xmjddz' && c.isShow && !!c.display" />
                                    <van-field v-model="form.address" readonly :label="c.name" v-if="c.code === 'xmxxdz' && c.isShow && !!c.display" />
                                </div>
                                <ZdyView :zdyArryList="child.childList" :title="item.title" v-if="!!child.display" />
                            </template>
                            <template v-else-if="child.code === 'jzxx' && child.isShow && !!child.display">
                                <div class="s-name">{{ child.name }}</div>
                                <div v-for="(c, g) in child.childList" :key="g" class="bb1">
                                    <van-field v-model="form.houseOrlandDetailWebObj.builtYear" readonly :label="c.name" v-if="c.code === 'jcnf' && c.isShow && !!c.display" />
                                    <van-field :value="getOptionValue(form.houseOrlandDetailWebObj.buildingStructure)" readonly :label="c.name" v-if="c.code === 'jzjg' && shouldShowField(c)" />
                                    <van-field :value="formatBoolean(form.houseOrlandDetailWebObj.dilapidated)" readonly :label="c.name" v-if="c.code === 'sfwf' && shouldShowField(c)" />
                                    <van-field :value="formatBoolean(form.houseOrlandDetailWebObj.provisional)" readonly :label="c.name" v-if="c.code === 'sflj' && shouldShowField(c)" />
                                    <van-field v-model="form.houseOrlandDetailWebObj.contractorName" readonly :label="c.name" v-if="c.code === 'jzdw' && c.isShow && !!c.display" />
                                    <van-field :value="getOptionValue(form.houseOrlandDetailWebObj.quality)" readonly :label="c.name" v-if="c.code === 'fwzl' && shouldShowField(c)" />
                                </div>
                                <ZdyView :zdyArryList="child.childList" :title="item.title" v-if="!!child.display" />
                            </template>
                            <template v-else>
                                <div class="s-name" v-if="child.childList.length && !!child.display">{{ child.name }}</div>
                                <ZdyView :zdyArryList="child.childList" :title="item.title" v-if="!!child.display" />
                            </template>
                        </template>
                        <template v-else-if="item.title === '接收信息'">
                            <template v-if="child.code === 'lyxx' && child.isShow && !!child.display">
                                <div class="s-name">{{ child.name }}</div>
                                <div v-for="(c, g) in child.childList" :key="g" class="bb1">
                                    <van-field :value="getOptionValue(form.acceptWebDto.sourceId)" readonly :label="c.name" v-if="c.code === 'lylx' && shouldShowField(c)" />
                                    <van-field v-model="form.acceptWebDto.acquireDate" readonly :label="c.name" v-if="c.code === 'jzdw' && c.isShow && !!c.display" />
                                    <van-field v-model="form.acceptWebDto.unitName" readonly :label="c.name" v-if="c.code === 'yjdw' && c.isShow && !!c.display" />
                                    <van-field v-model="form.acceptWebDto.acquireArea" readonly :label="c.name" v-if="c.code === 'jssmj' && c.isShow && !!c.display" />
                                </div>
                                <ZdyView :zdyArryList="child.childList" :title="item.title" v-if="!!child.display" />
                            </template>
                            <template v-else>
                                <div class="s-name" v-if="child.childList.length && !!child.display">{{ child.name }}</div>
                                <ZdyView :zdyArryList="child.childList" :title="item.title" v-if="!!child.display" />
                            </template>
                        </template>
                        <template v-else-if="item.title === '价值信息'">
                            <template v-if="child.code === 'jzxx' && child.isShow && !!child.display">
                                <div class="s-name">{{ child.name }}</div>
                                <div v-for="(c, g) in child.childList" :key="g" class="bb1">
                                    <van-field v-model="form.valueWebDto.price" readonly :label="c.name" v-if="c.code === 'zcjz' && c.isShow && !!c.display" />
                                    <van-field v-model="form.valueWebDto.unitPrice" readonly :label="c.name" v-if="c.code === 'zcdj' && c.isShow && !!c.display" />
                                    <van-field v-model="form.valueWebDto.postDate" readonly :label="c.name" v-if="c.code === 'rzrq' && c.isShow && !!c.display" />
                                    <van-field v-model="form.valueWebDto.originalPrice" readonly :label="c.name" v-if="c.code === 'zmyz' && c.isShow && !!c.display" />
                                    <van-field v-model="form.valueWebDto.originalBasis" readonly :label="c.name" v-if="c.code === 'yzyj' && c.isShow && !!c.display" />
                                    <van-field :value="formatBoolean(form.valueWebDto.onAccount)" readonly :label="c.name" v-if="c.code === 'sfzz' && shouldShowField(c)" />
                                    <van-field v-model="form.valueWebDto.onAccountUnit" readonly :label="c.name" v-if="c.code === 'zzdw' && shouldShowField(c)" />
                                    <van-field :value="getOptionValue(form.valueWebDto.accountSubject)" readonly :label="c.name" v-if="c.code === 'jzkm' && shouldShowField(c)" />
                                </div>
                                <ZdyView :zdyArryList="child.childList" :title="item.title" />
                            </template>
                            <template v-else>
                                <div class="s-name" v-if="child.childList.length && !!child.display">{{ child.name }}</div>
                                <ZdyView :zdyArryList="child.childList" :title="item.title" v-if="!!child.display" />
                            </template>
                        </template>
                        <template v-else-if="item.title === '产证信息'">
                            <template v-if="child.code === 'cqxx' && child.isShow && !!child.display">
                                <div class="s-name">{{ child.name }}</div>
                                <div v-for="(c, g) in child.childList" :key="g" class="bb1">
                                    <van-field :value="formatRightState(form.houseOrlandDetailWebObj.certificateWebDto.rightState)" readonly :label="c.name" v-if="c.code === 'cqqk' && shouldShowField(c)" />
                                    <van-field :value="getOptionValue(form.houseOrlandDetailWebObj.certificateWebDto.rightIds)" readonly :label="c.name" v-if="c.code === 'czqk' && shouldShowField(c)" />
                                    <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.noRightReason" readonly :label="c.name" v-if="c.code === 'qzyy' && c.isShow && !!c.display" />
                                    <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.rightUnitId"  @click-right-icon="goxzcq" right-icon="arrow" readonly :label="c.name" v-if="c.code === 'cqdw' && c.isShow && !!c.display" />
                                    <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.landNumber" readonly :label="c.name" v-if="c.code === 'zdh' && c.isShow && !!c.display" />
                                    <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.rightRatio" readonly :label="c.name" v-if="c.code === 'cqbl' && c.isShow && !!c.display" />
                                    <van-field :value="getOptionValue(form.houseOrlandDetailWebObj.certificateWebDto.rightTypeId)" readonly :label="c.name" v-if="c.code === 'cqlb' && shouldShowField(c)" />
                                    <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.rightNumber" readonly :label="c.name" v-if="c.code === 'cqzh' && c.isShow && !!c.display" />
                                    <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.identifyNumber" readonly :label="c.name" v-if="c.code === 'cjh' && c.isShow && !!c.display" />
                                    <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.issuedDate" readonly :label="c.name" v-if="c.code === 'bzrq' && c.isShow && !!c.display" />
                                    <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.houseBuildingArea" readonly :label="c.name" v-if="c.code === 'fwjzmjzj' && c.isShow && !!c.display" />
                                    <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.parcelArea" readonly :label="c.name" v-if="c.code === 'zdmjzj' && c.isShow && !!c.display" />
                                    <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.realEstateUnitNumber" readonly :label="c.name" v-if="c.code === 'bdcdyh' && c.isShow && !!c.display" />
                                </div>
                                <ZdyView :zdyArryList="child.childList" :title="item.title" />
                            </template>
                            <template v-else-if="child.code === 'dyxx' && child.isShow && !!child.display">
                                <div class="s-name">{{ child.name }}</div>
                                <div v-for="(c, g) in child.childList" :key="g" class="bb1">
                                    <van-field :value="formatBoolean(form.houseOrlandDetailWebObj.certificateWebDto.mortgaged)" readonly :label="c.name" v-if="c.code === 'sfdy' && shouldShowField(c)" />
                                    <template v-if="+form.houseOrlandDetailWebObj.certificateWebDto.mortgaged === 1">
                                        <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.mortgageeName" readonly :label="c.name" v-if="c.code === 'dyqr' && c.isShow && !!c.display" />
                                        <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.mortgageAmount" readonly :label="c.name" v-if="c.code === 'dyje' && c.isShow && !!c.display" />
                                        <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.mortgageBeginDate" readonly :label="c.name" v-if="c.code === 'dyksrq' && c.isShow && !!c.display" />
                                        <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.mortgageEndDate" readonly :label="c.name" v-if="c.code === 'dyjsrq' && c.isShow && !!c.display" />
                                        <van-field :value="getOptionValue(form.houseOrlandDetailWebObj.certificateWebDto.mortgageMethod)" readonly :label="c.name" v-if="c.code === 'dyfs' && shouldShowField(c)" />
                                        <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.creditAmount" readonly :label="c.name" v-if="c.code === 'bdbzzqse' && c.isShow && !!c.display" />
                                        <van-field v-model="form.houseOrlandDetailWebObj.certificateWebDto.creditTopAmount" readonly :label="c.name" v-if="c.code === 'zgzqse' && c.isShow && !!c.display" />
                                    </template>
                                </div>
                                <ZdyView :zdyArryList="child.childList" :title="item.title" />
                            </template>
                            <template v-else>
                                <div class="s-name" v-if="child.childList.length && !!child.display">{{ child.name }}</div>
                                <ZdyView :zdyArryList="child.childList" :title="item.title" v-if="!!child.display" />
                            </template>
                        </template>
                        <template v-else-if="item.title === '经营信息'">
                            <template v-if="child.code === 'jyxx' && child.isShow && !!child.display">
                                <div class="s-name">{{ child.name }}</div>
                                <div v-for="(c, g) in child.childList" :key="g" class="bb2">
                                    <van-field style="padding: 12px !important" :value="formatBoolean(form.houseOrlandDetailWebObj.operateWebDto.planned)" readonly :label="c.name" v-if="c.code === 'sfwczcgh' && shouldShowField(c)" />
                                </div>
                                <ZdyView :zdyArryList="child.childList" :title="item.title" v-if="!!child.display" />
                            </template>
                            <template v-else>
                                <div class="s-name" v-if="child.childList.length && !!child.display">{{ child.name }}</div>
                                <ZdyView :zdyArryList="child.childList" :title="item.title" v-if="!!child.display" />
                            </template>
                        </template>
                        <!-- 自定义展示 -->
                        <template v-else>
                            <div class="s-name" v-if="!!child.display">{{ child.name }}</div>
                            <ZdyView :zdyArryList="child.childList" :title="item.title" v-if="!!child.display" />
                        </template>
                    </div>
                </van-form>
            </van-tab>
        </van-tabs>

        <van-popup v-model="mapShow">
            <positionMap v-if="form.positionType === 1" ref="positionMap" :initialPosition="initialPosition" />
            <gaoDeMap v-if="form.positionType === 2" ref="positionMap" :initialPosition="initialPosition" />
        </van-popup>
    </div>
</template>

<script>
import backMixin from '../../mixins/backMixin'
import BaiduMap from 'vue-baidu-map'
import positionMap from '@/components/positionMap'
import UploadFile from '@/components/uploadFile'
import ZdyView from './zdy.vue'
import dayjs from 'dayjs'
import Vue from 'vue'
import { BaiduGaoDeLngLatConvert, isPlusEnv } from '@/utils/utils'
export default {
    mixins: [backMixin],
    name: 'pmd',
    data() {
        return {
            mapShow: false,
            form: null,
            initialPosition: {}, // 经纬度
            mapObj: {
                point: {}
            }, // 地图的经纬度
            addressObj: {
                name: '',
                isShow: false
            },
            orgObj: {
                name: '',
                isShow: false
            },
            jzmjObj: {
                name: '',
                isShow: false
            },
            jcnfObj: {
                name: '',
                isShow: false
            },
            tableDataCQ: [], // 产权附件
            tableData: [], // 相关附件
            zctpArr: [], // 资产图片
            tabTit: [], // 标签页标题列表
            fieldList: [], // 字段列表
            customizeList: [], // 自定义字段列表
            projectTreeData: [],
            isAPP: false,
            hasMatchingNodeId: false,
            baseInfoList: [],
             isLoadingData: false, // 防止重复请求的标志
            isInitialized: false, // 防止初始化时重复调用
            lastRouteId: null, // 记录上次的路由 id
        needRefresh: false // 新增：手动刷新标志
        }
    },
    components: {
        UploadFile,
        ZdyView,
        positionMap
    },
    computed: {
        mapConfig() {
            return this.$store.state.baseData.mapConfig
        }
    },
    created() {
        this.isAPP = isPlusEnv() || false
    },
    mounted() {
        this.init()
    },
    watch: {
         '$route.query.k': {
        handler(newId) {
            if (newId && !this.isLoadingData) {
                // 如果id变化或需要刷新，则重新加载
                if (this.lastRouteId !== newId || this.needRefresh) {
                    this.lastRouteId = newId
                    this.needRefresh = false
                    this.init()
                }
            }
        },
        immediate: true
    }
    },
    methods: {
            // 刷新
    handleRefresh() {
        this.needRefresh = true
        this.init()
    },
        goZclx(){ //资产类型
            // 带上 returnName 确保选择页可以通过路由回传 backData
            this.$router.push({name:'assectType', params:{key:'zclx'}, query: { returnName: this.$route.name }} )
        },
        goGlzc(){//关联资产
            this.$router.push({name:'assetCorrelation', params:{key:'zclx'}})
        },
        goxzcq(){//选中产权
            this.$router.push({name:'selectUnit'})
        },
        goZcwz(){//资产位置
            this.$router.push({name:'assectLocation'})
        },
        /**
         * 格式化日期
         */
        formatDate(val) {
            if (!val) return ''
            return dayjs(new Date(+val)).format('YYYY-MM-DD')
        },

        /**
         * 判断字段是否应该显示
         */
        shouldShowField(field) {
            return field.isShow && !!field.display
        },

        /**
         * 根据标题获取字段列表
         */
        getFieldListByTitle(title) {
            const fieldGroup = this.fieldList.find((e) => e.name === title)
            return fieldGroup?.childList || []
        },

        /**
         * 获取选项值（从optionMap中获取）
         */
        getOptionValue(key) {
            if (!key || !this.form?.optionMap) return ''
            return this.form.optionMap[key] || ''
        },

        /**
         * 格式化布尔值显示
         */
        formatBoolean(value) {
            if (![0, 1].includes(+value)) return '--'
            return +value === 0 ? '否' : '是'
        },

        /**
         * 格式化权利状态
         */
        formatRightState(value) {
            if (value === 2) return '无证'
            if (value === 1) return '有证'
            return '未知'
        },
        /**
         * 初始化页面
         */
         async init() {
        const id = this.$route.query.k
        if (!id || this.isLoadingData) return
        
        this.isLoadingData = true
        try {
            await this.getDetailData(id)
            this.isInitialized = true
        } catch (error) {
            console.error('初始化失败:', error)
        } finally {
            this.isLoadingData = false
        }
    },

        /**
         * APP环境初始化
         */
        async handleAppInit(id) {
            const { orgId, memberId, XUserToken } = this.$route.query
            localStorage.XUserToken = XUserToken

            const resToken = await this.$Geting(this.$api.getTokenByAppServiceId, {
                appServiceId: 'property',
                orgId,
                memberId
            })

            if (resToken.isSuccess) {
                sessionStorage.accessToken = resToken.data.accessToken
                await this.getOrgProjectTree()
                await this.getDetailData(id)
            }
        },

        /**
         * 获取楼盘项目树
         */
        async getOrgProjectTree() {
            try {
                const res = await this.$Geting(this.$api.getAssetsTree)
                if (res.code === '0') {
                    let resData = res.data.treeData || {}
                    this.projectTreeData = resData ? [resData] : []
                }
            } catch (error) {
                console.error('获取楼盘项目树出错:', error)
            }
        },
        /**
         * 获取百度地图配置
         */
        async queryMapConfig(tenantId) {
            let res = await this.$Geting(this.$api.getMapBaiDu + '?tenantId=' + tenantId)
            if (res.isSuccess) {
                let resData = res.data
                let { jsKey } = resData
                this.$ak = jsKey
                this.$store.commit('setMapConfig', resData)
                Vue.use(BaiduMap, { ak: jsKey })
            }
        },
        /**
         * 处理字段列表，设置显示状态并生成标签页
         */
        async processFieldList(fieldTreeList, showDataList, customizeList, showAll) {
            this.tabTit = []
            // 处理字段显示状态
            fieldTreeList.forEach((group) => {
                if (group.childList?.length) {
                    group.childList.forEach((category) => {
                        if (category.childList?.length) {
                            category.childList.forEach((field) => {
                                // 根据显示数据列表设置字段显示状态
                                if (showDataList.length) {
                                    field.isShow = showDataList.some((data) => {
                                        if (field.customize) {
                                            return field.fieldRule.descPath === data.fieldKeyPath
                                        }
                                        return field.path === data.fieldKeyPath
                                    })
                                } else {
                                    field.isShow = showAll || field.display
                                }

                                // 更新特殊字段对象
                                this.updateSpecialFieldObjects(field)
                            })
                        }
                    })
                }
            })
            // 处理自定义字段显示状态
            if (customizeList.length) {
                customizeList.forEach((customField) => {
                    customField.isShow = showDataList.length ? showDataList.some((data) => customField.descPath === data.fieldKeyPath) : true
                })

                // 更新自定义字段值
                fieldTreeList.forEach((group) => {
                    group.childList?.forEach((category) => {
                        category.childList?.forEach((field) => {
                            if (field.customize) {
                                const customField = customizeList.find((cf) => cf.descPath === field.fieldRule.descPath)
                                if (customField) {
                                    field.value = customField.value
                                }
                            }
                        })
                    })
                })
            }

            // 获取基本信息列表
            const baseInfoGroup = fieldTreeList.find((e) => e.name === '基本信息')
            this.baseInfoList = baseInfoGroup?.childList || []

            // 更新父组件的显示状态
            fieldTreeList.forEach((node) => {
                this.updateParentIsShow(node)
            })

            // 生成标签页标题
            this.tabTit = this.generateTabTitles(fieldTreeList)
            this.fieldList = fieldTreeList
        },

        /**
         * 更新特殊字段对象
         */
        updateSpecialFieldObjects(field) {
            const specialFields = {
                gldw: () => {
                    this.orgObj.isShow = field.isShow || field.display
                },
                tjsl: () => {
                    this.jzmjObj = {
                        name: field.name,
                        isShow: field.isShow || field.display
                    }
                },
                xmxxdz: () => {
                    this.addressObj.isShow = field.isShow || field.display
                },
                jcnf: () => {
                    this.jcnfObj.isShow = field.isShow || field.display
                }
            }

            if (specialFields[field.code]) {
                specialFields[field.code]()
            }
        },

        /**
         * 生成标签页标题列表
         */
        generateTabTitles(fieldTreeList) {
            const titles = []
            const idSet = new Set()

            fieldTreeList.forEach((group, index) => {
                if (group.isShow && group.display) {
                    const hasChildList = group.childList?.some((c) => c.childList?.length)
                    if (hasChildList) {
                        const id = (index + 1).toString()
                        if (!idSet.has(id)) {
                            idSet.add(id)
                            titles.push({
                                title: group.name,
                                isShow: group.isShow,
                                id
                            })
                        }
                    }
                }
            })

            return titles
        },

        /**
         * 递归更新父组件的isShow属性
         */
        updateParentIsShow(node) {
            if (node.childList && node.childList.length > 0) {
                let hasTrue = false
                node.childList.forEach((child) => {
                    this.updateParentIsShow(child)
                    if (child.isShow) {
                        hasTrue = true
                    }
                })
                node.isShow = hasTrue
            }
        },
        /**
         * 递归检查节点ID是否匹配
         */
        checkNodeIdMatch(node, orgId) {
            if (node.nodeId === orgId) {
                return true
            }
            if (node.childList?.length) {
                return node.childList.some((child) => this.checkNodeIdMatch(child, orgId))
            }
            return false
        },

        /**
         * 获取资产详情数据
         */
        async getDetailData(id) {
          
            const res = await this.$Geting(this.$api.getAssetsRegisterById + '?k=' + id)
            if (res.code !== '0') {
                this.$toast(res.message)
                this.$router.back()
                return
            }

            const { customizeWebDto } = res.data
            const businessDto = customizeWebDto.businessDto

            // 检查是否有匹配的节点ID（APP环境才能展示所有数据）
            if (this.projectTreeData.length) {
                this.hasMatchingNodeId = this.projectTreeData.some((node) => this.checkNodeIdMatch(node, businessDto.orgId))
            }

            // 获取地图配置
            await this.queryMapConfig(businessDto.tenantId)

            // 处理自定义数据
            const customizeList = customizeWebDto.customizeList || []
            this.processCustomizeData(customizeList)

            // 处理房屋/土地详情
            if (businessDto.category === 0) {
                businessDto.houseOrlandDetailWebObj = businessDto.houseDetailWebDto
            } else if (businessDto.category === 1) {
                businessDto.houseOrlandDetailWebObj = businessDto.landDetailWebDto
            }

            // 处理附件数据
            this.processAttachments(businessDto.attachmentWebDtoList || [])

            // 处理证书日期
            if (businessDto.houseOrlandDetailWebObj?.certificateWebDto) {
                this.processCertificateDates(businessDto.houseOrlandDetailWebObj.certificateWebDto)
            }

            // 获取字段列表
            let showDataList = []
            if (this.hasMatchingNodeId) {
                await this.processFieldList(res.data.fieldTreeWebDtoList, [], customizeList, true)
            } else {
                showDataList = await this.getShowData(businessDto.typeId, businessDto.tenantId)
                await this.processFieldList(res.data.fieldTreeWebDtoList, showDataList, customizeList, false)
            }

            // 设置表单数据
            this.form = businessDto
            this.orgObj.name = businessDto.orgName
            this.jcnfObj.name = businessDto.houseOrlandDetailWebObj?.builtYear
            this.addressObj.name = businessDto.address
console.log(businessDto.latitude,'businessDto.latitude')
            // 设置地图位置
            if (businessDto.latitude) {
                this.initialPosition = {
                    lng: businessDto.longitude,
                    lat: businessDto.latitude
                }
                this.getMapCoordinate(this.initialPosition)
            }
         
        },

        /**
         * 处理自定义数据
         */
        processCustomizeData(customizeList) {
            customizeList.forEach((item) => {
                if (item.ctrlType === 'checkbox') {
                    item.value = item.value?.length ? item.value.split(',') : []
                }
                item.descPathArr = item.descPath.split(',')
            })
        },

        /**
         * 处理附件数据
         */
        processAttachments(attachmentList) {
            attachmentList.forEach((item) => {
                const attachData = item.attachData ? JSON.parse(item.attachData) : []

                if (+item.belongType === 0 && attachData.length) {
                    // 产权附件
                    this.tableDataCQ = attachData.map((e) => ({ ...e, url: e.fileUrl }))
                } else if (+item.belongType === 1 && attachData.length) {
                    // 相关附件
                    this.tableData = attachData.map((e) => ({ ...e, url: e.fileUrl }))
                } else if (+item.belongType === 2 && attachData.length) {
                    // 资产图片
                    this.zctpArr = attachData
                }
            })
        },

        /**
         * 处理证书日期
         */
        processCertificateDates(certificateDto) {
            if (certificateDto.issuedDate) {
                certificateDto.issuedDate = this.formatDate(certificateDto.issuedDate)
            }
            if (certificateDto.mortgageEndDate) {
                certificateDto.mortgageEndDate = this.formatDate(certificateDto.mortgageEndDate)
            }
            if (certificateDto.mortgageBeginDate) {
                certificateDto.mortgageBeginDate = this.formatDate(certificateDto.mortgageBeginDate)
            }
        },

        /**
         * 获取显示数据
         */
        async getShowData(typeId, tenantId) {
            try {
                const res = await this.$Geting(this.$api.getsavePublic + '?id=' + typeId + '&tenantId=' + tenantId)
                if (res.code === '0') {
                    return res.data
                }
            } catch (error) {
                console.error('请求字段列表出错:', error)
            }
            return []
        },

        /**
         * 获取地图坐标
         */
        getMapCoordinate(v) {
            console.log(v)
            if (this.form.latitude || v) {
                console.log('122')
                let [lng, lat] = [this.form.latitude, this.form.longitude] || [v.latitude, v.longitude]
                let obj = { lng, lat, positionType: this.form.positionType || 1 }
                // 对数据作坐标转换
                obj = BaiduGaoDeLngLatConvert(obj, this.mapConfig.type, 'location')
                this.initialPosition = { lng: obj.lng, lat: obj.lat }
                this.mapObj = {
                    point: { lng: obj.lng, lat: obj.lat }
                }
                this.$nextTick(() => {
                    this.$refs.mapWrap && this.$refs.mapWrap.handleClick(this.mapObj)
                })
            }
        },
        /**
         * 查看地图
         */
        viewMapClick() {
            this.mapShow = true
            this.$nextTick(() => {
                this.$refs.positionMap?.handleClick(this.mapObj)
            })
        }
    }
}
</script>

<style lang="less" scoped>
.p-detail-box {
    height: 100vh;
    background: #f5f6f8;
    padding-bottom: 30px;
    overflow: scroll;

    .color86 {
        color: #868b97;
    }

    .my-swipe {
        width: 100vw;
        height: 212px;

        ::v-deep .van-image {
            width: 100%;
            height: 100%;

            img {
                width: 100%;
                height: 100%;
            }
        }
    }

    .bgfff {
        background: #fff;
    }

    .p-title {
        font-size: 17px;
        font-weight: 600;
        color: #171e33;
        margin: 0 0 16px;
    }

    .addr-bg {
        width: 100%;
        height: 67px;
        background: url('../../assets/addrbg.png') no-repeat center/100%;
        position: relative;

        img {
            position: absolute;
            width: 34px;
            height: 34px;
            top: 6px;
            right: 12px;
        }

        .p-addr {
            padding: 12px 0 0 12px;
            margin-bottom: 0;
        }
    }

    .p-addr2 {
        padding: 12px 0 0 12px;
    }

    .flex-bc {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .flex1 {
        flex: 1;
    }

    .p-addr {
        color: #171e33;
        font-size: 14px;
        margin-bottom: 10px;
    }

    .p-t-box {
        padding: 16px 12px;
    }

    .p16 {
        padding: 16px;
    }

    .ptb16 {
        padding: 16px 0;
    }

    .mlr16 {
        margin: 0 16px;
    }

    ::v-deep .new-tabs-box {
        .van-tab {
            flex: none;
            border: 1px solid #c4ccdc;
            margin-left: 10px;
            padding: 6px 12px;
            font-size: 14px;
            height: 32px;
            color: #4c505a;
            border-radius: 4px;
        }

        // .van-tabs__nav {
        //     .van-tab:first-child {
        //         margin-left: 0;
        //     }
        // }

        .van-tabs__nav--line.van-tabs__nav--complete {
            padding-left: 12px;
        }

        .van-tab--active {
            background: #3772ff !important;
            color: #fff !important;
            border-color: #3772ff;
        }

        .van-tabs__wrap {
            position: relative;

            &::after {
                content: '';
                width: 100%;
                position: absolute;
                bottom: 0;
                left: 0;
                border-bottom: 1px solid #3772ff;
            }
        }

        .van-tabs__line {
            background-color: #fff;
            width: 5px;

            &::before {
                content: '';
                position: absolute;
                left: 50%;
                width: 5px;
                height: 5px;
                top: 2px;
                background-color: #fff;
                border-top: 1px solid #3772ff;
                border-bottom: 1px solid #3772ff;
                border-left: 1px solid #3772ff;
                border-right: 1px solid #3772ff;
                transform: rotateZ(45deg) translateX(-50%);
            }
        }
    }

    ::v-deep .van-cell {
        padding: 0 0 12px 0 !important;

        .van-cell__title.van-field__label {
            color: #868b97;
            font-size: 14px;
        }

        .van-field__control {
            font-size: 14px;
            color: #171e33;
            font-weight: 500;
        }
    }

    ::v-deep .is-upload {
        border-radius: 8px;

        .custom-title,
        .file-arrow,
        .custom-tip {
            display: none;
        }

        .van-cell {
            border-radius: 8px;
        }

        .file-tit {
            font-size: 14px;
            color: #171e33 !important;
            padding-left: 16px !important;
        }

        .file-info {
            padding: 16px;
        }
    }

    .item-box {
        padding: 0 12px;

        .s-name {
            font-size: 15px;
            line-height: 18px;
            font-weight: 600;
            padding: 14px 0 10px;
            position: relative;
            z-index: 1;

            &::after {
                content: '';
                width: 60px;
                height: 8px;
                background: linear-gradient(90deg, #3772ff 0%, rgba(245, 246, 248, 0) 100%);
                position: absolute;
                bottom: 12px;
                left: 0;
                z-index: -1;
            }
        }

        .bb1:nth-child(2) {
            border-radius: 8px 8px 0 0;
            padding-top: 16px;
        }
    }

    .item-box1 {
        .bb1:last-child {
            border-radius: 0 0 8px 8px;
            padding-bottom: 6px;

            .van-cell {
                display: none;
            }
        }
    }

    .bb1 {
        padding: 0 12px;
        background: #fff;
        position: relative;
    }

    .disn {
        height: 20px;
    }

    .c-img {
        width: 297px;
        height: 160px;
        background: url('../../assets/no.png') no-repeat center/100%;
        margin: 20px auto 0;
    }

    .c-f {
        font-weight: 500;
        font-size: 14px;
        color: #d8d8d8;
        padding-top: 12px;
        text-align: center;
    }

    .e-item2 {
        margin: 16px 12px 0;
    }

    .e-item {
        background: #ffffff;
        padding: 16px 12px;
        border-radius: 10px;
        font-weight: 500;
        font-size: 14px;

        .e-top {
            color: #868b97;
        }

        .e-txt {
            padding-top: 20px;
            color: #171e33;
        }
    }
}
</style>
